package view;

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import view.actionListeners.AddKingsListener;

import controller.Controller;

/**
 * Class represents a panel to add the Kings for each player on partial game mode
 */
public class AddKingsPanel extends JPanel {

	// members
	
	// set serializing id
	private static final long serialVersionUID = 1L;
	
	// button strings
	private static final String CONFIRM_STRING = "Confirm";
	
	// combo-box to choose position
	private JComboBox<Integer> firstRowIndexBox;
	private JComboBox<Integer> firstColIndexBox;
	private JComboBox<Integer> secondRowIndexBox;
	private JComboBox<Integer> secondColIndexBox;
	private Integer[] indices = {1, 2, 3, 4, 5, 6, 7, 8};

	// action button
	private JButton confirmBtn;
	
	// inner panels
	private JPanel firstKingPanel;
	private JPanel secondKingPanel;
	private JPanel confirmPanel;
	private JPanel mainPanel;
	
	// public methods
	
	/**
	 * Constructor- creates a new panel to adds Kings
	 * @param frameManager- object to manage the frame
	 * @param gameManager- object to manage the game
	 * @param piecePanel- panel to add the rest of the pieces
	 */
	public AddKingsPanel(FrameManager frameManager, Controller gameManager, AddPiecePanel piecePanel) {
		
		// set combo-boxes for position
		firstRowIndexBox = new JComboBox<Integer>(indices);
		firstColIndexBox = new JComboBox<Integer>(indices);
		secondRowIndexBox = new JComboBox<Integer>(indices);
		secondColIndexBox = new JComboBox<Integer>(indices);
		
		// place combo-boxes on panel
		JLabel firstRowLable = new JLabel("Row");
		JLabel firstColLable = new JLabel("Column");
		JLabel secondRowLable = new JLabel("Row");
		JLabel secondColLable = new JLabel("Column");
		firstKingPanel = new JPanel(new GridLayout(4,1));
		secondKingPanel = new JPanel(new GridLayout(4,1));
		firstKingPanel.add(firstRowLable);
		firstKingPanel.add(firstRowIndexBox);
		firstKingPanel.add(firstColLable);
		firstKingPanel.add(firstColIndexBox);
		secondKingPanel.add(secondRowLable);
		secondKingPanel.add(secondRowIndexBox);
		secondKingPanel.add(secondColLable);
		secondKingPanel.add(secondColIndexBox);
		
		// set action button
		confirmBtn = new JButton(CONFIRM_STRING);
		confirmBtn.setActionCommand(CONFIRM_STRING);
		
		// set listener
		AddKingsListener listener = new AddKingsListener(gameManager, frameManager, this, piecePanel);
		confirmBtn.addActionListener(listener);
		
		// place action button on panel
		confirmPanel = new JPanel();
		confirmPanel.add(confirmBtn);
		
		// set panel borders
		firstKingPanel.setBorder(BorderFactory.createTitledBorder("White King"));
		secondKingPanel.setBorder(BorderFactory.createTitledBorder("Black King"));
		
		// add components to main panel
		mainPanel = new JPanel(new GridLayout(3,1,10,10));
		mainPanel.add(firstKingPanel);
		mainPanel.add(secondKingPanel);
		mainPanel.add(confirmPanel);
		add(mainPanel);
	}
	
	/**
	 * Returns selected row index
	 * @return value of selected row index
	 */
	public int getFirstRowIndex() { return firstRowIndexBox.getSelectedIndex() + 1; }
	
	/**
	 * Returns selected column index
	 * @return value of selected column index
	 */
	public int getFirstColIndex() { return firstColIndexBox.getSelectedIndex() + 1; }
	
	/**
	 * Returns selected row index
	 * @return value of selected row index
	 */
	public int getSecondRowIndex() { return secondRowIndexBox.getSelectedIndex() + 1; }
	
	/**
	 * Returns selected column index
	 * @return value of selected column index
	 */
	public int getSecondColIndex() { return secondColIndexBox.getSelectedIndex() + 1; }
}